<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="weaver.general.Util"%>
<%@ page import="weaver.formmode.service.RepeatVerifyService"%>
<%@ page import="weaver.hrm.HrmUserVarify"%>
<%@ page import="weaver.conn.RecordSet"%>
<%@ page import="weaver.common.util.taglib.ShowColUtil"%>
<%@ page import="java.util.*"%>
<%@ page import="weaver.hrm.User"%>
<%@ page import="java.util.Map.Entry"%>
<%@ page import="weaver.formmode.search.editplugin.AbstractPluginElement"%>
<%@ page import="weaver.formmode.search.editplugin.PluginElementClassName"%>
<%@ page import="weaver.formmode.service.CustomSearchService"%>
<%@ page import="weaver.formmode.search.CustomSearchBatchEditUtil"%>
<%@page import="com.weaver.formmodel.util.StringHelper" %>
<%@ page import="java.util.Iterator"%>
<%@ page import="net.sf.json.JSONArray"%>
<%@ page import="net.sf.json.JSONObject"%>
<%@ page import="weaver.formmode.setup.ModeRightInfo"%>
<%@ page import="com.engine.cube.biz.ShardingActionHelper" %>
<jsp:useBean id="FormModeRightInfo" class="weaver.formmode.search.FormModeRightInfo" scope="page" />
<%

out.clear();
User user = HrmUserVarify.getUser(request,response);
RecordSet rs =  new RecordSet();
String action = Util.null2String(request.getParameter("action"));
if("cleanColWidthByUser".equalsIgnoreCase(action)){
	String customid = Util.null2String(request.getParameter("customid"));
	boolean isRight = false;
	rs.executeSql("select * from mode_searchPageshareinfo where righttype=1 and pageid = " + StringHelper.empty2Null(customid));
	if(rs.next()){  
		FormModeRightInfo.setUser(user);
		isRight = FormModeRightInfo.checkUserRight(Util.getIntValue(customid),1);
	}else{  //没有设置任何查看权限数据，则认为有权限查看
		isRight = true;
	}
	if(!isRight){
		response.sendRedirect("/notice/noright.jsp");
	 	return;
	}
	if(!"".equals(customid)){
		String pageId = "mode_customsearch:" + customid;
		String sql = "delete from user_default_col where pageid = '"+pageId+"' and userid = "+user.getUID()+"";
		rs.execute(sql);
		sql = "update system_default_col set isdefault=1 where pageid = '"+pageId+"'";//初始化列宽将所有isdefault重置为1
		rs.execute(sql);
		ShowColUtil.reloadCache(pageId);
		ShowColUtil.removeDefaultColsMap(pageId);
	}
	JSONObject jsonObject = new JSONObject();
	jsonObject.put("status", "1");
	out.print(jsonObject.toString());
	return;
}else if("getVerifyField".equalsIgnoreCase(action)){
	int customid = Util.getIntValue(Util.null2String(request.getParameter("customid")), 0);
	int modeid = 0;
	int formid = 0;
	String tablename = "";
	String detailtable = "";
	String detailkeyfield = "";
	String sql = "select a.modeid,a.formid,a.detailtable,b.tablename,b.detailkeyfield from mode_customsearch a left join workflow_bill b on a.formid=b.id where a.id="+customid;
	rs.executeSql(sql);
	if(rs.next()){
		modeid = Util.getIntValue(rs.getString("modeid"), 0);
		formid = Util.getIntValue(rs.getString("formid"), 0);
		tablename = Util.null2String(rs.getString("tablename"));
		detailtable=Util.null2String(rs.getString("detailtable"));
		detailkeyfield = Util.null2String(rs.getString("detailkeyfield"));
	}
	RepeatVerifyService rvService = new RepeatVerifyService();
	rvService.setUser(user);
	JSONObject jsonObject = new JSONObject();
	jsonObject.put("VerifyField", rvService.getVerifyField(formid));
	
	String elementClassName = PluginElementClassName.getPrimaryKeyElementClassName();
	AbstractPluginElement pluginElement = (AbstractPluginElement)Class.forName(elementClassName).newInstance();
	String primaryKeyName = "id";
	String primaryKeyPluginName = pluginElement.getEditPluginName(primaryKeyName);
	String detailPrimaryKeyName = "d_id";
	String detailPrimaryKeyPluginName = pluginElement.getEditPluginName(detailPrimaryKeyName);
	jsonObject.put("primaryKeyPluginName", primaryKeyPluginName);
	jsonObject.put("detailPrimaryKeyPluginName", detailPrimaryKeyPluginName);
	out.print(jsonObject.toString());
	return;
}else if("VerifyData".equalsIgnoreCase(action)){
	RepeatVerifyService rvService = new RepeatVerifyService();
	String data = Util.null2String(request.getParameter("data"));
	int customid = Util.getIntValue(Util.null2String(request.getParameter("customid")), 0);
	int modeid = 0;
	int formid = 0;
	String tablename = "";
	String detailtable = "";
	String detailkeyfield = "";
	String sql = "select a.modeid,a.formid,a.detailtable,b.tablename,b.detailkeyfield from mode_customsearch a left join workflow_bill b on a.formid=b.id where a.id="+customid;
	rs.executeSql(sql);
	if(rs.next()){
		modeid = Util.getIntValue(rs.getString("modeid"), 0);
		formid = Util.getIntValue(rs.getString("formid"), 0);
		tablename = Util.null2String(rs.getString("tablename"));
		detailtable=Util.null2String(rs.getString("detailtable"));
		detailkeyfield = Util.null2String(rs.getString("detailkeyfield"));
	}
	data = data.replace("+", "%2B");

	JSONArray dataArr = JSONArray.fromObject(data);
	rvService.setUser(user);
	rvService.initVerifyField(formid);
	rvService.completeVerifyFieldData(dataArr, tablename, detailtable);
	JSONObject jsonObject = rvService.verifyDataSelf(dataArr, tablename, detailtable,"0");
	if(!jsonObject.getBoolean("verify")){
		jsonObject = rvService.verifyData(dataArr, tablename, detailtable);
	}
	out.print(jsonObject.toString());
	return;
}else if("batchEdit".equalsIgnoreCase(action)){
	int customid = Util.getIntValue(Util.null2String(request.getParameter("batcheditCustomid")), 0);
	String modifiedRows = Util.null2String(request.getParameter("modifiedRows"));
	String clientaddress = request.getRemoteAddr();
	boolean isBatchEdit = false;
	rs.executeSql("select * from mode_searchPageshareinfo where righttype=2 and pageid = " + customid);
	if(rs.next()){
		FormModeRightInfo.setUser(user);
		isBatchEdit = FormModeRightInfo.checkUserRight(Util.getIntValue(String.valueOf(customid)),2);
	}
	if(!isBatchEdit){
		response.sendRedirect("/notice/noright.jsp");
	 	return;
	}
	int modeid = 0;
	int formid = 0;
	String tablename = "";
	String detailtable = "";
	String detailkeyfield = "";
	String sql = "select a.modeid,a.formid,a.detailtable,b.tablename,b.detailkeyfield from mode_customsearch a left join workflow_bill b on a.formid=b.id where a.id="+customid;
	rs.executeSql(sql);
	if(rs.next()){
		modeid = Util.getIntValue(rs.getString("modeid"), 0);
		formid = Util.getIntValue(rs.getString("formid"), 0);
		tablename = Util.null2String(rs.getString("tablename"));
		detailtable=Util.null2String(rs.getString("detailtable"));
		detailkeyfield = Util.null2String(rs.getString("detailkeyfield"));
	}
	CustomSearchService customSearchService = new CustomSearchService();
	List<Map<String,Object>> editableFields = customSearchService.getEditableFieldsById(customid);
	Map<String,Object> editableFieldMap = new HashMap<String,Object>();
	AbstractPluginElement pluginElement = null;
	for(int i=0;i<editableFields.size();i++){
		Map<String,Object> editableField = editableFields.get(i);
		int fieldId = Util.getIntValue(Util.null2String(editableField.get("id")), 0);
		String fieldhtmltype = Util.null2String(editableField.get("fieldhtmltype"));
		String fieldtype = Util.null2String(editableField.get("type"));
		String elementClassName = PluginElementClassName.getElementClassName(fieldhtmltype,fieldtype);
		pluginElement = (AbstractPluginElement)Class.forName(elementClassName).newInstance();
		String pluginName = pluginElement.getEditPluginName(fieldId);
		editableFieldMap.put(pluginName, editableField);
	}
	boolean batchEditResult = false;
	StringBuffer batchEditMessage = new StringBuffer();
	int dialogW = 300;
	int dialogH = 80;
	CustomSearchBatchEditUtil customSearchBatchEditUtil  = new CustomSearchBatchEditUtil();
	Map<String,Object> needLogFieldsMap = customSearchBatchEditUtil.getNeedLogFields(formid);
	Map<String,Object> modeRightRelatedFieldsMap = new HashMap<String,Object>();
	JSONObject modifiedRowsObject = JSONObject.fromObject(modifiedRows);
	JSONObject mRows = modifiedRowsObject.getJSONObject("mrows");
	Iterator rowKey = mRows.keys();
	List<String> billList = new ArrayList<String>();
	List<Integer> modeList = new ArrayList<Integer>();
	// CHANGE START
	List<String> formBizIdList = new ArrayList<>();
	// CHANGE END
	while(rowKey.hasNext()){
		String rowIndex = Util.null2String(rowKey.next());
		JSONObject rowObject = mRows.getJSONObject(rowIndex);
		String updateMaintableSql = "";
		String updateDetailtableSql = "";
		boolean ismodifyneedlogfield = false;
		boolean ismodifyRelatedField = false;
		customSearchBatchEditUtil.resetNeedLogFields(needLogFieldsMap);
		// CHANGE START
		String suffix = "";
		String mainid = Util.null2String(rowObject.get("id"));//主表id
		String formBizId = mainid;
		boolean isModeSharding = ShardingActionHelper.isModeSharding(modeid);
		if (isModeSharding) {
			mainid = ShardingActionHelper.extractBillId(formBizId);
			suffix = ShardingActionHelper.getShardingSuffix(formBizId);
		}
		// CHANGE END
		String detailid = Util.null2String(rowObject.get("detailid"));
		JSONArray fields = rowObject.getJSONArray("fields");
		rs.executeSql("select formmodeid from " + tablename + " where id = " + mainid);
		if(!rs.next())continue;
		if(modeid==0){
			modeid = Util.getIntValue(rs.getString("formmodeid"), 0);
		}
		if(modeid==0)continue;
		Map<String,Object> rightRelatedFieldsMap = null;
		if(modeRightRelatedFieldsMap.containsKey(String.valueOf(modeid))){
			rightRelatedFieldsMap = (Map<String,Object>)modeRightRelatedFieldsMap.get(String.valueOf(modeid));
		}else{
			rightRelatedFieldsMap = customSearchBatchEditUtil.getRightRelatedFields(modeid);
			modeRightRelatedFieldsMap.put(String.valueOf(modeid), rightRelatedFieldsMap);
		}
		
		Map<String,Object> modifyRightRelatedFieldsMap = new HashMap<String,Object>();
		
		Vector mainV = new Vector();
		Vector detailV = new Vector();
		
		for(int j=0;j<fields.size();j++){
			JSONObject fieldObject = fields.getJSONObject(j);
			String pluginName = Util.null2String(fieldObject.get("pluginname"));
			if(!editableFieldMap.containsKey(pluginName)) continue;
			Map<String,Object> editableField = (Map<String,Object>)editableFieldMap.get(pluginName);
			String fieldId = Util.null2String(editableField.get("id"));
			String fieldName = Util.null2String(editableField.get("fieldname"));
			String fieldHtmlType = Util.null2String(editableField.get("fieldhtmltype"));
			String fieldType = Util.null2String(editableField.get("type"));
			String fieldDbType = Util.null2String(editableField.get("fielddbtype"));
			int viewtype = Util.getIntValue(Util.null2String(editableField.get("viewtype")), 0);
			
			String fieldValue = Util.null2String(fieldObject.get("value"));
			String oldFieldValue = Util.null2String(fieldObject.get("oval"));
			Object realFieldValue = customSearchBatchEditUtil.AnalyzeStorageValue(fieldValue, fieldHtmlType, fieldType, fieldDbType);
			boolean isEqualVal = customSearchBatchEditUtil.judgeEqualFieldValue(fieldHtmlType,fieldType,oldFieldValue, realFieldValue,fieldValue);
			if(isEqualVal) continue;
            //记录日志字段
            if(needLogFieldsMap.containsKey(fieldId)){
            	JSONObject jsonObject = (JSONObject)needLogFieldsMap.get(fieldId);
            	jsonObject.put("nfieldvalue", realFieldValue);
            	jsonObject.put("ofieldvalue", oldFieldValue);
            	ismodifyneedlogfield = true;
            }
            
            //判断权限关联字段值是否改变
            if(rightRelatedFieldsMap.containsKey(fieldId)){
            	modifyRightRelatedFieldsMap.put(fieldId, rightRelatedFieldsMap.get(fieldId));
            	ismodifyRelatedField = true;
            }
			
			if(viewtype==0){
				updateMaintableSql += ","+fieldName+"=?";
				mainV.addElement(realFieldValue);
			}else{
				if(!"".equals(detailid)){
					updateDetailtableSql += ","+fieldName+"=?";
					detailV.addElement(realFieldValue);
				}else{
					updateDetailtableSql += ","+fieldName+"";
					detailV.addElement(realFieldValue);
				}
			}
		}
		
		if(mainV.size()>0){
			updateMaintableSql = updateMaintableSql.substring(1);
			updateMaintableSql = "update " + tablename + suffix + " set " + updateMaintableSql + " where id = " + mainid;
			boolean uResult = rs.executeSql(updateMaintableSql,false,mainV);
			if(uResult && ismodifyneedlogfield){
				customSearchBatchEditUtil.saveLogFieldsModifyInfo(Util.getIntValue(mainid,0),detailid,mainid, modeid, user, clientaddress, "5", needLogFieldsMap);
			}
		}
		if(detailV.size()>0){
			if(!"".equals(detailid)){
				updateDetailtableSql = updateDetailtableSql.substring(1);
				updateDetailtableSql = "update " + detailtable + suffix + " set " + updateDetailtableSql + " where id = " + detailid;
				rs.executeSql(updateDetailtableSql,false,detailV);
			}else{
				updateDetailtableSql = "insert into " + detailtable + suffix + " (mainid" + updateDetailtableSql + ") values (" + mainid;
				for(int k=0;k<detailV.size();k++){
					updateDetailtableSql += ",?";
				}
				updateDetailtableSql += ")";
				rs.executeSql(updateDetailtableSql,false,detailV);
			}
		}
		if(!billList.contains(mainid)){
			billList.add(mainid);
			modeList.add(modeid);
			formBizIdList.add(formBizId);
		}
	}
	
	for(int i=0;i<billList.size();i++){
		String bid = billList.get(i);
		int mid = modeList.get(i);
		String formBizId = formBizIdList.get(i);
		ModeRightInfo ModeRightInfo = new ModeRightInfo();
		ModeRightInfo.setSuffix(ShardingActionHelper.getShardingSuffix(formBizId));
		ModeRightInfo.rebuildModeDataShareByEdit(-1,mid,Util.getIntValue(bid,0));
		ModeRightInfo.addDocShare(-1,mid,Util.getIntValue(bid,0));
	}
	if(batchEditMessage.length()==0){
		batchEditMessage.append("更新成功，是否继续修改？");
	}
	JSONObject resultJson = new JSONObject();
	resultJson.put("resultMessage", batchEditMessage.toString());
	out.print("<script>parent.customSearchOperate.batchEditCallbak("+resultJson+","+dialogW+","+dialogH+")</script>");
}
%>